﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HorizontalAlignment = System.Windows.HorizontalAlignment;

namespace Heyo.Controls.Animations
{
    public class WaveClick : AnimationLayer
    {

        [Category("Animation")]
        public Brush WaveBrush
        {
            get => (Brush)GetValue(WaveBrushProperty);
            set => SetValue(WaveBrushProperty, value);
        }

        public static readonly DependencyProperty WaveBrushProperty = DependencyProperty.Register(
            "WaveBrush", typeof(Brush), typeof(WaveClick), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 0, 0, 0)),
                (s, e) => { ((WaveClick)s)._ellipseWave.Fill = (Brush)e.NewValue; }));


        private readonly DoubleAnimation _clickWaveAnim = new DoubleAnimation() { From = 5 };
        private readonly DoubleAnimation _clickWaveOpacityAnim = new DoubleAnimation();
        private readonly PowerEase _easeFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut, Power = 4 };
        private readonly Ellipse _ellipseWave;

        public WaveClick()
        {
            Loaded += MaterialCard_Loaded;
            UseLayoutRounding = true;

            _clickWaveAnim.EasingFunction
               = _clickWaveOpacityAnim.EasingFunction
                       = _easeFunction;


            _clickWaveOpacityAnim.SpeedRatio = AnimationSpeed / 2;

            _clickWaveAnim.SpeedRatio
                = AnimationSpeed;



            MouseEnter += (s, e) => { BeginMouseEnterAnim(e); };
            MouseLeave += (s, e) => { BeginMouseLeaveAnim(e); };

            MouseDown += MaterialCard_MouseDown;



            _ellipseWave = new Ellipse
            {
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Top,
                RenderTransform = new ScaleTransform(),
                RenderTransformOrigin = new Point(0.5, 0.5),
                Height = 0,
                Width = 0,
            };

            CanvasAnimation.Children.Add(_ellipseWave);
            CanvasAnimation.ClipToBounds = true;

        }
        protected override void OnRender(DrawingContext dc)
        {
            OnApplyChildClip();
            base.OnRender(dc);
        }

        protected override void OnApplyChildClip()
        {

            ClipRect.RadiusX = ClipRect.RadiusY = Math.Max(0.0, CornerRadius.TopLeft - (BorderThickness.Left * 0.5));
            Rect rect = new Rect(RenderSize);
            rect.Height = Math.Max(0, rect.Height-(BorderThickness.Top + BorderThickness.Bottom));
            rect.Width = Math.Max(0, rect.Width - BorderThickness.Left + BorderThickness.Right);
            ClipRect.Rect = rect;
            CanvasAnimation.Clip = ClipRect;
        }
        private void MaterialCard_Loaded(object sender, RoutedEventArgs e)
        {
            CanvasAnimation.Opacity = 1;
            //CanvasAnimation.Background = ActiveBrush;
            _ellipseWave.Fill = WaveBrush;
        }



        private void BeginMouseEnterAnim(System.Windows.Input.MouseEventArgs e)
        {
            if (!MouseEnterAnimation) return;


            var ca = new DoubleAnimation()
            {
                To = 1,
                SpeedRatio = AnimationSpeed * 1.5,
                EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseOut, Power = 4 }
            };


            CanvasAnimation.BeginAnimation(Canvas.OpacityProperty, ca);

        }

        private void BeginMouseLeaveAnim(System.Windows.Input.MouseEventArgs e)
        {
            if (!MouseEnterAnimation) return;
            Point p = e.GetPosition(this);
            if (ClipRect.FillContains(p))
                return;

            var ca = new DoubleAnimation()
            {
                To = 0,
                SpeedRatio = AnimationSpeed * 1.5,
                EasingFunction = new PowerEase() { EasingMode = EasingMode.EaseOut, Power = 4 }
            };

            CanvasAnimation.BeginAnimation(Canvas.OpacityProperty, ca);

        }



        private void MaterialCard_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!MouseDownWave) return;

            Point p = e.GetPosition((IInputElement)sender);
            Canvas.SetLeft(_ellipseWave, p.X);
            Canvas.SetTop(_ellipseWave, p.Y);

            double radius = Math.Max(ActualHeight * 1.41, ActualWidth * 1.41) * 2;
            _ellipseWave.Width = _ellipseWave.Height = 10; ;
            _clickWaveAnim.To = radius / 10;
            _clickWaveOpacityAnim.From = 1;
            _clickWaveOpacityAnim.To = 0;

            _ellipseWave.RenderTransform.BeginAnimation(ScaleTransform.ScaleXProperty, _clickWaveAnim);
            _ellipseWave.RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, _clickWaveAnim);
            _ellipseWave.BeginAnimation(OpacityProperty, _clickWaveOpacityAnim);
        }

    }
}